from django.urls import reverse
from rest_framework.test import APIClient
from django.db.models import Sum
from play.models import Play
from video.models import Videos
from django.test import TestCase
from user.models import UserProfile
from like.models import Like


class TestLikeViewSet(TestCase):
    def setUp(self):
        self.client = APIClient()
        self.user1 = UserProfile.objects.create_user(id=1, username='testuser1', password='123456456')
        self.user2 = UserProfile.objects.create_user(id=2, username='testuser2', password='123456456')
        self.user3 = UserProfile.objects.create_user(id=3, username='testuser3', password='123456456')
        self.video1 = Videos.objects.create(vid=1, title='test1', duration=10, mup=self.user1, type = '1')
        self.video2 = Videos.objects.create(vid=2, title='test2', duration=10, mup=self.user1, type = '2')
        self.video3 = Videos.objects.create(vid=3, title='test3', duration=10, mup=self.user1, type = '2')
        self.like1 = Like.objects.create(lid=1, user_id=self.user1, video_id=self.video1, is_active=True)
        self.like2 = Like.objects.create(lid=2, user_id=self.user2, video_id=self.video1, is_active=True)

    def test_get_like_count_exists(self):
        url = reverse('like-count', kwargs={'pk':1, 'user':1})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['like_count'], 2)
        self.assertEqual(data['is_liked'], True)

    def test_get_like_count_not_exists(self):
        url = reverse('like-count', kwargs={'pk':1, 'user':3})
        response = self.client.get(url)
        data = response.json()
        self.assertEqual(data['like_count'], 2)
        self.assertEqual(data['is_liked'], False)
